new Vue({
    el:'#addProduct',
    data:{
        infoFlag:false,
        infoText:'',
        product:{
            id:'',
            name:'',
            description:'',
            price:'',
            stock:'',
            categoryLevel1Id:'',
            categoryLevel2Id:'',
            categoryLevel3Id:'',
        },
        firstLevel:[],
        secondLevel:[],
        thirdLevel:[],
        firstType:0,
        secondType:0,
        thirdType:0,
        addImg:null,
        nameExist:false,
    },
    methods:{
        getFile(event)
        {
            this.addImg=event.target.files[0]
        },
        getAddLevel1() {

            this.firstLevel=[]
            axios.get('/api/admin/getUpdateLevel',
                {
                    params:{
                        type:1,
                        id:1
                    }
                }).then(rs=>{
                this.firstLevel=rs.data.list
            })
        },
        getAddLevel2()
        {
            this.secondType=0
            this.thirdType=0
            this.secondLevel=[]
            axios.get('/api/admin/getUpdateLevel',
                {
                    params:{
                        type:2,
                        id:this.firstType
                    }
                }).then(rs=>{
                this.secondLevel=rs.data.list
            })
        },
        getAddLevel3()
        {
            this.thirdLevel=[]
            axios.get('/api/admin/getUpdateLevel',
                {
                    params:{
                        type:3,
                        id:this.secondType
                    }
                }).then(rs=>{
                this.thirdLevel=rs.data.list
            })
        },
        addSave()
        {
            // this.nameExist=false
            if(this.product.name==="")
            {
                this.infoFlag=true
                this.infoText="商品名不能为空"
                return
            }
            if(this.product.price==='')
            {
                this.infoFlag=true
                this.infoText="单价不能为空"
                return
            }
            if(this.product.stock==='')
            {
                this.infoFlag=true
                this.infoText="库存不能为空"
                return
            }
            if(isNaN(this.product.price))
            {
                this.infoFlag=true
                this.infoText="单价为数字类型"
                return
            }
            if(isNaN(this.product.stock))
            {
                this.infoFlag=true
                this.infoText="库存为数字类型"
                return
            }
            if(this.firstType===0)
            {
                this.infoFlag=true
                this.infoText="请选则商品分类"
                return
            }
            // if(this.secondLevel.length!==0 && this.secondType===0)
            // {
            //     this.infoFlag=true
            //     this.infoText="该一级分类下存在子级分类，不能添加"
            //     return
            // }
            // if(this.thirdLevel.length!==0 && this.thirdType===0)
            // {
            //     this.infoFlag=true
            //     this.infoText="该二级分类下存在子级分类，不能添加"
            //     return
            // }
            const formData = new FormData();
            if(this.addImg==null)
            {
                this.infoFlag=true
                this.infoText="请添加商品图"
                return
            }
            axios.get('/api/admin/checkProductName',{
                params:{
                    name:this.product.name,
                    categoryLevel1Id:this.firstType,
                    categoryLevel2Id:this.secondType,
                    categoryLevel3Id:this.thirdType,
                }
            }).then(rs=>{

                if(rs.data.isExist==="yes")
                {
                    this.infoText="商品名已存在"
                    this.infoFlag=true
                }else {
                    formData.append('fileImg', this.addImg);
                    this.product.categoryLevel1Id=this.firstType
                    this.product.categoryLevel2Id=this.secondType
                    this.product.categoryLevel3Id=this.thirdType
                    formData.append('product', JSON.stringify(this.product)); // 转换为字符串
                    axios.post('/api/admin/addProduct', formData, {
                        headers: {
                            'Content-Type': 'multipart/form-data'
                        }
                    }).then(rs=>{
                        this.infoFlag=true
                        if(rs.data.isAdd==="success")
                        {
                            this.infoText="添加成功"
                            setTimeout(()=>{
                                window.location="/api/ProductManage.html"
                            },500)
                        }
                        if(rs.data.isAdd==="fail")
                        {
                            this.infoText="添加出错了"
                        }
                        if(rs.data.info==="noImg")
                        {
                            this.infoText="商品图是必须是图片格式"
                        }
                        if(rs.data.info==="outSize")
                        {
                            this.infoText="商品图不能超过5MB"
                        }
                    })
                }
            })

        },
        closeInfo()
        {
            this.infoFlag=false
        },
    },
    created()
    {
        this.getAddLevel1()
    }
})